// -------------------------------------------------------------------------
//    @FileName			:    {Filename}
//    @Author           :    GaoYi
//    @Date             :    2017-09-27
//    @Module           :   {Filename}
//
// -------------------------------------------------------------------------

#include "PointTurnMgr.h"
#include "CenterService.h"
#include "Common/ComDefine.pb.h"

bool PointTurnMgr::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	if (nullptr != pMessageDispatch)
	{
		pMessageDispatch->registerClientHandler(EMODULE_ID_WELFARE, this);
		pMessageDispatch->registerServerHandler(EMODULE_ID_WELFARE, this);
	}

	return true;
}

bool PointTurnMgr::UnInit()
{
	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	if (nullptr != pMessageDispatch)
	{
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_WELFARE);
		pMessageDispatch->unregisterServerHandler(EMODULE_ID_WELFARE);
	}
	return true;
}

void PointTurnMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case LOGIC_TO_CENTER_SEND_POINT_TURN_CHOUJIANG:
		SendPointTurnChouJiang(package);
		break;
	default:
		LogErrFmtPrint("center account Mgr didnt recv this msg, cmd:%d, moduleId:%d", actionId, package.moduleId);
		break;
	}
}

bool PointTurnMgr::SendPointTurnChouJiang(RECV_PACKAGE& package)
{
	SendPointTurnChouJiangInfo rsp;

	if (!rsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[logic] PointTurnMgr::SendPointTurnChouJiang SendPointTurnChouJiangInfo  parse failed...");
		return false;
	}

	if (m_listInfo.size() > 10)
	{
		m_listInfo.pop_front();
	}
	m_listInfo.push_back(rsp.player_info());
	return true;
}

void PointTurnMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id /* = 0 */)
{
	if (package.session == nullptr)
	{
		LogErrFmtPrint(" PointTurnMgr::onClientMessage, session null, module:%d, cmd:%d", package.moduleId, package.cmd);
		return;
	}

	switch (actionId)
	{
	case CLIENT_TO_CENTER_POINT_TURN_LIST_INFO:
		GetPointTurnChouJiangInfoHandle(Id, package);
		break;
	default:
		break;
	}

}

bool PointTurnMgr::GetPointTurnChouJiangInfoHandle(CharIDType cid, RECV_PACKAGE& package)
{
	PlayerPointTurnChouJiangListReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] PlayerPointTurnChouJiangListReq return but parse failed....");
		return false;
	}

	PlayerPointTurnChouJiangListRep rsp;

	for (auto it = m_listInfo.begin(); it != m_listInfo.end(); it++)
	{
		rsp.add_player_info()->CopyFrom(*it);
	}

	g_GetCenterService()->SendDataToClient(cid, (uint16_t)EMODULE_ID_WELFARE, (uint16_t)CENTER_TO_CLIENT_POINT_TURN_LIST_INFO, &rsp);
	return true;
}